import axios from "axios";
import { Message } from "element-ui";

const URL_API = 'http://39.106.25.167:8098/'//正式地址
// const URL_API = "http://192.168.1.106:8091/"; //健池
// const URL_API = 'http://47.105.174.187:8098/'//备用地址
// const URL_API = 'http://192.168.203.142:8090/'
// const URL_API = 'http://192.168.200.47:8090/'
// const URL_API = 'http://192.168.1.104:8090/'   //健池
// const URL_API = 'http://192.168.1.147:8090/'   //郑

// 请求头带token
// var tt = JSON.parse(localStorage.getItem("userInfo"))
//   axios.defaults.headers.common['token'] = tt.token;
//设置超时时间
axios.defaults.timeout = 50000;
axios.interceptors.request.use(
  config => {
    if (JSON.parse(localStorage.getItem("userInfo"))) {
      config.headers["token"] = JSON.parse(
        localStorage.getItem("userInfo")
      ).token;
    }
    return config;
  },
  error => {
    // do something with request error
    console.log("出错了");
    return Promise.reject(error);
  }
);

// if(localStorage.getItem('token')){
//   axios.defaults.headers.common['Authorization'] = localStorage.getItem('token');
// }

// post请求头
axios.defaults.headers.post["Content-Type"] =
  "application/x-www-form-urlencoded;charset=UTF-8";
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';

/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */

export function get(url, params = {}) {
  // params.token = localStorage.getItem("token")
  return new Promise((resolve, reject) => {
    axios
      .get(URL_API + url, {
        params: params
      })
      .then(response => {
        if (response.data.msg === "token不能为空!") {
          window.location.href = "/";
        } else if (response.data.msg === "未找到登录信息!") {
          window.location.href = "/";
        } else {
          resolve(response.data);
        }
        // if(response.data.success){
        //   this.$message({
        //     message: response.data.message,
        //     type: 'success'
        //   });
        // }else{
        //   this.$message.error(response.data.message);
        // }
      })
      .catch(err => {
        // this.$message.error("请求失败，请联系管理员");
        reject(err);
      });
  });
}

/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function post(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.post(URL_API + url, data).then(
      response => {
        if (response.data.msg === "token不能为空!") {
          window.location.href = "/";
        } else if (response.data.msg === "未找到登录信息!") {
          window.location.href = "/";
        } else {
          resolve(response.data);
        }
        // if(response.data.code == 200){
        //   this.$message({
        //     message: response.data.message,
        //     type: 'success'
        //   });
        // }else{
        //   this.$message.error(response.data.message);
        // }
        // resolve(response.data);
      },
      err => {
        // this.$message.error("请求失败，请联系管理员");
        reject(err);
      }
    );
  });
}
